﻿using Nasty.Core.LoadParams;
using Nasty.Core;
using Nasty.Core.Repository;
using Nasty.Core.SuperExtension;
using Nasty.SystemModule.Areas.SerialNumberConfig.Model;
using Nasty.SystemModule.Areas.SerialNumberConfig.Params;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace Nasty.SystemModule.SerialNumberConfig
{
    public interface ISerialNumberConfigRepository : IRepository<Nasty.Core.Entity.System.SerialNumberConfig>
    {
        public Nasty.Core.Entity.System.SerialNumberConfig GetSerialNumberConfig(string id);

        public ResultData<Nasty.Core.Entity.System.SerialNumberConfig> SaveSerialNumberConfig(SerialNumberConfigModel model);

        public PageData<Nasty.Core.Entity.System.SerialNumberConfig> GetSerialNumberConfigPage(GetSerialNumberConfigPageParams @params);

        public List<Nasty.Core.Entity.System.SerialNumberConfig> GetSerialNumberConfigs(GetSerialNumberConfigsParams @params);

        public ResultData<string> DeleteSerialNumberConfig(string id);
    }

    public class SerialNumberConfigRepository : SqlRepository<Nasty.Core.Entity.System.SerialNumberConfig>, ISerialNumberConfigRepository
    {
        public SerialNumberConfigRepository(SqlSugarClient db) : base(db)
        {
            //db.CodeFirst.InitTables(typeof(SerialNumberConfig));
            //db.CodeFirst.InitTables(typeof(SerialNumberConfigRole));
            //db.CodeFirst.InitTables(typeof(Role.Role));
            //db.CodeFirst.InitTables(typeof(RolePermission));
            //db.CodeFirst.InitTables(typeof(Permission.Permission));
            //db.CodeFirst.InitTables(typeof(Permission.PermissionGroup));
        }

        public Nasty.Core.Entity.System.SerialNumberConfig GetSerialNumberConfig(string id)
        {
            return this.Db.Queryable<Nasty.Core.Entity.System.SerialNumberConfig>().IncludesAllFirstLayer().InSingle(id);
        }

      

         public PageData<Nasty.Core.Entity.System.SerialNumberConfig> GetSerialNumberConfigPage(GetSerialNumberConfigPageParams @params)
        {
            int totalPage = 0;
            int total = 0;
            var pageData = new PageData<Nasty.Core.Entity.System.SerialNumberConfig>();

            var _SQLExpress = Db.Queryable<Nasty.Core.Entity.System.SerialNumberConfig>().IncludesAllFirstLayer();

            if (!string.IsNullOrEmpty(@params.Name)) _SQLExpress.Where((t) => t.Name.Contains(@params.Name));
            if (!string.IsNullOrEmpty(@params.Code)) _SQLExpress.Where((t) => t.Code.Contains(@params.Code));

            _SQLExpress = _SQLExpress.OrderBy((t) => t.CreateTime, OrderByType.Desc);

            var data = _SQLExpress.ToPageList(@params.Current, @params.PageSize, ref total, ref totalPage);

            pageData.Total = total;
            pageData.TotalPage = totalPage;
            pageData.Data = data;

            pageData.Current = @params.Current;
            pageData.PageSize = @params.PageSize;
            return pageData;
        }

        public List<Nasty.Core.Entity.System.SerialNumberConfig> GetSerialNumberConfigs(GetSerialNumberConfigsParams @params)
        {

            var _SQLExpress = Db.Queryable<Nasty.Core.Entity.System.SerialNumberConfig>().IncludesAllFirstLayer();

            if (!string.IsNullOrEmpty(@params.Name)) _SQLExpress.Where((t) => t.Name.Contains(@params.Name));
            if (!string.IsNullOrEmpty(@params.Code)) _SQLExpress.Where((t) => t.Code.Contains(@params.Code));

            _SQLExpress = _SQLExpress.OrderBy((t) => t.CreateTime, OrderByType.Desc);

            return _SQLExpress.ToList();
        }

        public ResultData<Nasty.Core.Entity.System.SerialNumberConfig> SaveSerialNumberConfig(SerialNumberConfigModel model)
        {
            var result = new ResultData<Nasty.Core.Entity.System.SerialNumberConfig>();
            try
            {
                var data = Db.Save<Nasty.Core.Entity.System.SerialNumberConfig>(model);
                result.IsSuccess = true;
                result.Data = data;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
                return result;
            }
        }

        public ResultData<string> DeleteSerialNumberConfig(string id)
        {
            var result = new ResultData<string>();
            try
            {
                Db.Delete<Nasty.Core.Entity.System.SerialNumberConfig>(id);
                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
                return result;
            }
        }
    }
}
